<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">

    <el-form-item label="物品类型" prop="type">
      <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="物品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="物品名称"></el-input>
    </el-form-item>
    <el-form-item label="参考价值" prop="money">
      <el-input type="number" v-model="dataForm.money" placeholder="参考价值"></el-input>
    </el-form-item>
    <el-form-item label="客人类型" prop="krType">
      <el-select v-model="dataForm.krType" placeholder="请选择" @change="changtype">
          <el-option
            v-for="item in krTypelist"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>

      <el-form-item label="请选择" prop="sa" v-if="dataForm.krType!='2'" >
        <!-- <el-input v-model="dataForm.roomId" style="width: 200px;" placeholder="房间号"></el-input>
          <el-input v-model="dataForm.userid" style="width: 200px;" placeholder="用户"></el-input> -->
        <el-autocomplete v-model="dataForm.sa" style="width: 220px;" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
      </el-form-item>

      <el-form-item label="房间号" prop="pfno"  >
        <el-input disabled v-model="dataForm.pfno" placeholder="房间号" v-if="dataForm.krType!='2'"></el-input>
         <el-input  v-model="dataForm.pfno" placeholder="房间号"  v-if="dataForm.krType=='2'"></el-input>
      </el-form-item>

      <el-form-item label="客人姓名" prop="fname">
        <el-input disabled v-model="dataForm.fname" placeholder="客人姓名"  v-if="dataForm.krType!='2'"></el-input>
          <el-input  v-model="dataForm.fname" placeholder="客人姓名"  v-if="dataForm.krType=='2'"></el-input>
      </el-form-item>

      <el-form-item label="电话" prop="ftel">
        <el-input disabled v-model="dataForm.ftel" placeholder="电话"  v-if="dataForm.krType!='2'"></el-input>
          <el-input  v-model="dataForm.ftel" placeholder="电话"  v-if="dataForm.krType=='2'"></el-input>
      </el-form-item>

      <el-form-item label="登记单号" prop="sdjdh">
        <el-input disabled v-model="dataForm.sdjdh" placeholder="登记单号"  v-if="dataForm.krType!='2'"></el-input>
          <el-input  v-model="dataForm.sdjdh" placeholder="登记单号"  v-if="dataForm.krType=='2'"></el-input>
      </el-form-item>

    <el-form-item label="备注" prop="remake">
      <el-input type="textarea" v-model="dataForm.remake" placeholder="备注"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        restaurants:[],
        krTypelist:[{
                  value: '0',
                  label: '在住客人'
                }, {
                  value: '1',
                  label: '离店客人'
                },{
                  value: '2',
                  label: '其他'
                }],
         options: [{
                  value: '1',
                  label: '贵重物品'
                }, {
                  value: '2',
                  label: '遗失物品'
                }],
        dataForm: {
          sa:'',
          id: 0,
          name: '',
          type: '',
          money: '',
          krType: '',
          fjr:'',
          remake: '',
          createUserId: '',
          createTime: '',
          updateUserId: '',
          updateTime: '',
          lqUserId: '',
          sellerId: '',
          pfno:'',
          fname:'',
          ftel:'',
          sdjdh:''
        },
        dataRule: {
          name: [
            { required: true, message: '物品名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '物品类型不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '价值不能为空', trigger: 'blur' }
          ],
          pfno: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          fname: [
            { required: true, message: '客人姓名不能为空', trigger: 'blur' }
          ],
          krType: [
            { required: true, message: '客人类型不能为空', trigger: 'blur' }
          ],
          sa: [
            { required: true, message: '房间号不能为空', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      changtype(type) {
          if (type == '0') {
              this.getList("1");
          } else if (type == '1') {
              this.getList("0");
          }
      },

      handleSelect(item) {
         this.dataForm.fjr = item.id;
         var name = item.value;
         var tel = item.label;
         var names = name.split("/");
         var tels = tel.split("/");
         this.dataForm.pfno = names[0];
         this.dataForm.fname = names[1];
         this.dataForm.ftel = tels[1];
         this.dataForm.sdjdh = tels[0];
      },
      querySearchAsync(queryString, cb) {

        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

        clearTimeout(this.timeout);
          cb(results);
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      getList(type) {

        this.$http({
          url: this.$http.adornUrl('/hotel/trentalgoods/selectroomplist/' + type),
          method: 'post'
        }).then(({
          data
        }) => {

          this.restaurants = data.page;
        });
      },
      init (id) {

          this.restaurants = [];
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tguestitems/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tGuestItems.name
                this.dataForm.type = data.tGuestItems.type
                this.dataForm.money = data.tGuestItems.money
                this.dataForm.krType = data.tGuestItems.krType
                this.dataForm.remake = data.tGuestItems.remake
                this.dataForm.createUserId = data.tGuestItems.createUserId
                this.dataForm.createTime = data.tGuestItems.createTime
                this.dataForm.updateUserId = data.tGuestItems.updateUserId
                this.dataForm.updateTime = data.tGuestItems.updateTime
                this.dataForm.lqUserId = data.tGuestItems.lqUserId
                this.dataForm.sellerId = data.tGuestItems.sellerId
                this.dataForm.pfno = data.tGuestItems.pfno
                this.dataForm.fname = data.tGuestItems.fname
                this.dataForm.ftel = data.tGuestItems.ftel
                this.dataForm.sdjdh = data.tGuestItems.sdjdh
                this.dataForm.sa = data.tGuestItems.sa
                 this.dataForm.fjr = data.tGuestItems.fjr
                         this.changtype(this.dataForm.krType);
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tguestitems/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'money': this.dataForm.money,
                'krType': this.dataForm.krType,
                'remake': this.dataForm.remake,
                'fjr': this.dataForm.fjr,
                'pfno': this.dataForm.pfno,
                'fname': this.dataForm.fname,
                'ftel': this.dataForm.ftel,
                'sdjdh': this.dataForm.sdjdh
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-dialog__wrapper /deep/ .el-dialog {
  width: 40%;
  .el-textarea__inner {
    height: 100px;
  }
}
@media only screen and (max-width: 1180px) {
  .el-dialog__wrapper /deep/ .el-dialog {
    width: 50%;
  }
}
</style>
